<template>
  <div id="userInfo">
    <el-container class="personContent">
      <el-main style="text-align:center; padding-top: 50px">
        <el-upload
          class="avatar-uploader"
          :action="this.$const.domain + '/v1/user/changeHeadImage'"
          :headers="headers"
          accept="image/jpeg, image/png, image/jpg"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          name="headImage"
        >
          <img
            v-if="user.headImage"
            :src="user.headImage"
            style="width: 150px; height: 150px;"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <div>
          <p style="display:inline-block;">昵称:</p>
          <h3
            v-if="nicknameVisable"
            style="display:inline-block; margin-left: 30px; margin-right: 40px"
          >{{user.nickname}}</h3>
          <el-input
            v-if="!nicknameVisable"
            v-model="nickname"
            :placeholder="user.nickname"
            style="display:inline-block; width: 100px; margin-left: 10px; margin-right: 10px"
          ></el-input>
          <div style="display:inline-block">
            <i @click="changeNickname()" class="el-icon-edit"></i>
          </div>
        </div>
        <div>
          <p style="display:inline-block;">用户名:</p>
          <h3 style="display:inline-block; margin-left: 25px; margin-right: 40px">{{user.username}}</h3>
        </div>
        <hr />
        <h1 style="margin: 200px auto">temp</h1>
      </el-main>
    </el-container>
    <Footer></Footer>
  </div>
</template>
<script>
import Footer from "@/components/subComponents/Footer";
import { myMixin } from "@/assets/js/mixin";
export default {
  mixins: [myMixin],
  components: {
    Footer
  },
  data() {
    return {
      user: {},
      nicknameVisable: true,
      nickname: ""
    };
  },
  methods: {
    changeNickname() {
      this.nicknameVisable = !this.nicknameVisable;
      if (this.nicknameVisable == true) {
        this.$axios
          .put(
            this.$const.domain +
              "/v1/user/changeNickname?nickname=" +
              this.nickname,
            null,
            {
              headers: this.headers
            }
          )
          .then(res => {
            if (res.data.code != 200) {
              this.$message(res.data.msg);
              return;
            }
            if (res.data.data == 1) {
              this.user.nickname = this.nickname;
              this.$store.commit("setUser", this.user);
            } else {
              this.$message.error("网络错误，不能访问");
            }
          })
          .catch(error => {
            console.log(error);
            this.$message.error("网络错误，不能访问");
          });
      }
    },
    handleAvatarSuccess(res, file) {
      this.user.headImage = file.response.data;
      this.$store.commit("setUserHeadImage", file.response.data);
    }
  },
  mounted() {
    this.user = this.$store.state.user;
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>